package cn.deschen.biz.service.impl;

import cn.deschen.biz.domain.entity.AdUnit;
import cn.deschen.biz.dto.unit.UnitAddDTO;
import cn.deschen.biz.dto.unit.UnitPageDTO;
import cn.deschen.biz.dto.unit.UnitQueryDTO;
import cn.deschen.biz.dto.unit.UnitUpdateDTO;
import cn.deschen.biz.mapper.AdUnitMapper;
import cn.deschen.biz.service.AdUnitEleService;
import cn.deschen.biz.service.AdUnitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 推广单元表 服务实现类
 * </p>
 *
 * @author deschen
 * @since 2021-06-02
 */
@Service
public class AdUnitServiceImpl extends ServiceImpl<AdUnitMapper, AdUnit> implements AdUnitService {

    @Autowired
    private AdUnitMapper unitMapper;

    @Autowired
    private AdUnitEleService unitEleService;

    @Autowired
    private MapperFacade mapperFacade;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(UnitAddDTO addDTO) {
        AdUnit unit = mapperFacade.map(addDTO, AdUnit.class);
        unitMapper.insert(unit);
        unitEleService.saveBatch(unit.getId(), addDTO.getEleMap());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void modify(UnitUpdateDTO updateDTO) {
        AdUnit unit = mapperFacade.map(updateDTO, AdUnit.class);
        unitMapper.updateById(unit);
        unitEleService.updateBatch(unit.getId(), updateDTO.getEleMap());
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeBatchByIds(List<Long> unitIds) {
        removeByIds(unitIds);
        unitEleService.removeBatchByUnitIds(unitIds);
    }

    @Override
    public List<UnitPageDTO> listByQueryDTO(UnitQueryDTO queryDTO) {
        return unitMapper.listByQueryDTO(queryDTO);
    }


}
